from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session

from api.schemas.stats import StatsResponse
from db.database import get_db
from services.stats_service import get_compilation_stats, get_platform_distribution
from loguru import logger

router = APIRouter()

@router.get("/", response_model=StatsResponse)
async def get_stats(db: Session = Depends(get_db)):
    """获取编译统计数据"""
    try:
        compilation_stats = get_compilation_stats(db)
        platform_distribution = get_platform_distribution(db)
        
        return {
            "compilation_stats": compilation_stats,
            "platform_distribution": platform_distribution
        }
    except Exception as e:
        logger.error(f"获取统计数据失败: {str(e)}")
        raise HTTPException(status_code=500, detail=f"获取统计数据失败: {str(e)}")
